package io.github.slideconflictdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

/**
 * Created by codeczx on 2018/4/10.
 */

public class HorizontalScrollViewEx2 extends ViewGroup {

    private static final String TAG = "HorizontalScrollViewEx2";

    public HorizontalScrollViewEx2(Context context) {
        super(context);
    }

    public HorizontalScrollViewEx2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalScrollViewEx2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept;
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            Log.i(TAG, "onInterceptTouchEvent: down");
            intercept = false;
        } else {
            Log.i(TAG, "onInterceptTouchEvent: move");
            intercept = true;
        }
        return intercept;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidth = 0;
        int measureHeight = 0;
        int childCount = getChildCount();
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.i(TAG, "onMeasure: widthSize:" + widthSize + " heightSize:" + heightSize);

        if (childCount == 0) {
            setMeasuredDimension(0, 0);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            measureWidth = getChildAt(0).getMeasuredWidth() * childCount;
            measureHeight = heightSize;
            setMeasuredDimension(measureWidth, measureHeight);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            measureWidth = widthSize;
            measureHeight = getChildAt(0).getMeasuredHeight();
            setMeasuredDimension(measureWidth, measureHeight);
        } else {
            measureWidth = getChildAt(0).getMeasuredWidth() * childCount;
            measureHeight = getChildAt(0).getMeasuredHeight();
            setMeasuredDimension(measureWidth, measureHeight);
            Log.i(TAG, "onMeasure: measureWidth measureHeight" + measureWidth + " " + measureHeight);
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout(left, 0, left + child.getMeasuredWidth(), child.getMeasuredHeight());
            left += child.getMeasuredWidth();
        }
    }
}
